package com.huizu.project.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.ProUserTer;
import com.huizu.project.domain.ProXxtz;
import com.huizu.project.mapper.ProUserMapper;
import com.huizu.project.mapper.ProUserTerMapper;
import com.huizu.project.mapper.ProXxtzMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huizu.project.mapper.ProUserDcMapper;
import com.huizu.project.domain.ProUserDc;
import com.huizu.project.service.IProUserDcService;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 督促信息Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-06-05
 */
@Service
public class ProUserDcServiceImpl implements IProUserDcService 
{
    @Autowired
    private ProUserDcMapper proUserDcMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private ProUserTerMapper proUserTerMapper;

    @Autowired
    private ProXxtzMapper proXxtzMapper;

    /**
     * 查询督促信息
     * 
     * @param id 督促信息主键
     * @return 督促信息
     */
    @Override
    public ProUserDc selectProUserDcById(String id)
    {
        return proUserDcMapper.selectProUserDcById(id);
    }

    /**
     * 查询督促信息列表
     * 
     * @param proUserDc 督促信息
     * @return 督促信息
     */
    @Override
    public List<ProUserDc> selectProUserDcList(ProUserDc proUserDc)
    {
        return proUserDcMapper.selectProUserDcList(proUserDc);
    }

    /**
     * 新增督促信息
     * 
     * @param proUserDc 督促信息
     * @return 结果
     */
    @Override
    public int insertProUserDc(ProUserDc proUserDc)
    {
        proUserDc.setCreateTime(DateUtils.getNowDate());
        return proUserDcMapper.insertProUserDc(proUserDc);
    }

    /**
     * 修改督促信息
     * 
     * @param proUserDc 督促信息
     * @return 结果
     */
    @Override
    public int updateProUserDc(ProUserDc proUserDc)
    {
        return proUserDcMapper.updateProUserDc(proUserDc);
    }

    /**
     * 批量删除督促信息
     * 
     * @param ids 需要删除的督促信息主键
     * @return 结果
     */
    @Override
    public int deleteProUserDcByIds(String[] ids)
    {
        return proUserDcMapper.deleteProUserDcByIds(ids);
    }

    /**
     * 删除督促信息信息
     * 
     * @param id 督促信息主键
     * @return 结果
     */
    @Override
    public int deleteProUserDcById(String id)
    {
        return proUserDcMapper.deleteProUserDcById(id);
    }

    /**
     * 督促用户
     * @return
     */
    public AjaxResult dcSt(ProUserDc proUserDc){
        proUserDc.setTitle("督促");
        proUserDc.setId(String.valueOf(SnowFlake.nextId()));
        proUserDc.setCreateTime(new Date());
        int i = proUserDcMapper.insertProUserDc(proUserDc);
        return AjaxResult.success("督促成功");
    }

    /**
     * 督促用户
     * @return
     */
    public AjaxResult dcStList(Map<String,Object> map){
        List<ProUserDc> dcList = new ArrayList<>();
        String userId = String.valueOf(map.get("userId"));
        List<String> list = (List<String>)map.get("list");
        for (String s : list) {
            ProUserDc proUserDc = new ProUserDc();
            proUserDc.setUserId(userId);
            proUserDc.setTitle("督促");
            proUserDc.setContent(s);
            proUserDc.setId(String.valueOf(SnowFlake.nextId()));
            proUserDc.setCreateTime(new Date());
            dcList.add(proUserDc);
//            int i = proUserDcMapper.insertProUserDc(proUserDc);
        }
        proUserDcMapper.batchProUserDc(dcList);
        return AjaxResult.success("督促成功");
    }

    /**
     * 确认督促
     * @return
     */
    public AjaxResult subSt(ProUserDc proUserDc){
        proUserDc.setSubTime(new Date());
        proUserDc.setSubStatus("2");
        int i = proUserDcMapper.updateProUserDc(proUserDc);
        return AjaxResult.success("督促成功");
    }


    /**
     * 查询督促信息列表
     *
     * @param proUserDc 督促信息
     * @return 督促信息集合
     */
    public List<ProUserDc> selectProUserDcListApi(ProUserDc proUserDc){
        startPage();
        List<ProUserDc> proUserDcs = proUserDcMapper.selectProUserDcList(proUserDc);
        for (ProUserDc userDc : proUserDcs) {
            if (userDc.getXxStatus()==0){
                ProXxtz proXxtz = new ProXxtz();
                proXxtz.setId(String.valueOf(SnowFlake.nextId()));
                proXxtz.setUserId(proUserDc.getBeUserId());
                proXxtz.setpId(userDc.getId());
                proXxtz.setCreateTime(new Date());
                proXxtzMapper.insertProXxtz(proXxtz);
            }
        }
        return proUserDcs;
    }

    /**
     * 查询督促信息列表
     *
     * @param proUserDc 督促信息
     * @return 督促信息集合
     */
    public List<ProUserDc> selectDcListTerApi(ProUserDc proUserDc){
        List<ProXxtz> proXxtzList = new ArrayList<>();
        List<ProUserDc> proUserDcs = proUserDcMapper.selectProUserDcList(proUserDc);
//        ProUserTer proUserTer = proUserTerMapper.selectProUserTerByUserId(proUserDc.getTerId());
        for (ProUserDc userDc : proUserDcs) {
//            if (userDc.getXxStatus()==0){
//                ProXxtz proXxtz = new ProXxtz();
//                proXxtz.setId(String.valueOf(SnowFlake.nextId()));
//                proXxtz.setUserId(proUserDc.getBeUserId());
//                proXxtz.setpId(userDc.getId());
//                proXxtz.setCreateTime(new Date());
//                proXxtzList.add(proXxtz);
//            }
        }
        return proUserDcs;
    }

    @Override
    public int createDcXj(ProUserDc proUserDc) {
        proUserDc.setTitle("督促");
        proUserDc.setId(String.valueOf(SnowFlake.nextId()));
        proUserDc.setCreateTime(new Date());
        return proUserDcMapper.insertProUserDc(proUserDc);
    }
}
